/**
 * 
 */
package com.ieasy.module.oa.project.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ieasy.basic.dao.IBaseDao;
import com.ieasy.basic.exception.ServiceException;
import com.ieasy.basic.model.Msg;
import com.ieasy.basic.model.SystemContext;
import com.ieasy.basic.util.BeanUtils;
import com.ieasy.basic.util.StringUtil;
import com.ieasy.module.common.web.servlet.WebContextUtil;
import com.ieasy.module.oa.project.entity.ProjectRyListEntity;
import com.ieasy.module.oa.project.web.form.ProjectRyListForm;

/**
 * @author 杨浩泉
 *
 */
@Service @Transactional
public class ProjectRyListService implements IProjectRyListService {
	
	@Inject
	private IBaseDao<ProjectRyListEntity> basedaoProjectRy ;
	
	@Override
	public Msg addList(List<ProjectRyListForm> forms) {
		//删除现有
		if(null != forms && forms.size() > 0) {
			deleteByProjectId(forms.get(0));
		}
		for (ProjectRyListForm projectRyListForm : forms) {
			add(projectRyListForm) ;
		}
		return new Msg(true, "添加成功！");
	}
	
	@Override
	public Msg add(ProjectRyListForm form) {
		ProjectRyListEntity entity = new ProjectRyListEntity() ;
		BeanUtils.copyNotNullProperties(form, entity) ;
		entity.setCreateName(WebContextUtil.getCurrentUser().getUser().getEmp_name()) ;
		entity.setCreated(new Date()) ;
		
		this.basedaoProjectRy.add(entity) ;
		return new Msg(true, "添加成功！");
	}
	

	@Override
	public Msg deleteByProjectId(ProjectRyListForm form) {
		try {
			if(null != form.getProjectId() && !"".equals(form.getProjectId())) {
				this.basedaoProjectRy.executeSQL("delete from ieasy_oa_project_ry_list where projectId=?", form.getProjectId()) ;
				return new Msg(true, "删除成功！") ;
			}
		} catch (Exception e) {
			return new Msg(false, "删除失败！") ;
		}
		return null;
	}
	
	@Override
	public ProjectRyListForm get(ProjectRyListForm form) {
		ProjectRyListEntity entity = this.basedaoProjectRy.load(ProjectRyListEntity.class, form.getId()) ;
		BeanUtils.copyNotNullProperties(entity, form) ;
		return form ;
	}

	@Override
	public List<ProjectRyListForm> list(ProjectRyListForm form) {
		if(null != form.getSort()) {
			String[] personSorts = new String[]{"created"};
			if(StringUtil.arrayToString(personSorts).contains(form.getSort())) {
				SystemContext.setSort("t."+form.getSort()) ; SystemContext.setOrder(form.getOrder()) ;
			}
		}
		try {
			return this.find(form) ;
		} catch (Exception e) {
			throw new ServiceException("加载信息异常", e) ;
		}
	}

	private List<ProjectRyListForm> find(ProjectRyListForm form) {
		Map<String, Object> alias = new HashMap<String, Object>();
		String sql = "select " +
					 "t.id, t.ry, t.type, t.changeDate, t.created " +
				 	 "from ieasy_oa_project_ry_list t " +
				 	 "where 1=1 ";
		sql = addWhere(sql, form, alias) ;
		return this.basedaoProjectRy.listSQL(sql, alias, ProjectRyListForm.class, false) ;
	}

	private String addWhere(String hql, ProjectRyListForm form, Map<String, Object> params) {
		if (form.getProjectId() != null && !"".equals(form.getProjectId())) {
			hql += " and t.projectId=:projectId";
			params.put("projectId", form.getProjectId());
		}
		return hql;
	}

	

	
}
